package com.eregg.learn.netty.protocol.client.handler;

import com.eregg.learn.netty.protocol.protocol.response.LoginResponsePacket;
import com.eregg.learn.netty.protocol.session.Session;
import com.eregg.learn.netty.protocol.util.SessionUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class LoginResponseHandler extends SimpleChannelInboundHandler<LoginResponsePacket> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginResponsePacket loginResponsePacket) throws Exception {

        String userId = loginResponsePacket.getUserId();
        String userName = loginResponsePacket.getUserName();

        if(loginResponsePacket.isSuccess()){
            SessionUtil.bindSession(new Session(userId, userName), ctx.channel());
            System.out.println(String.format("用户[%s]登陆成功,用户ID[%s]", userName, userId));
        }else {
            System.out.println(String.format("用户[%s]登陆失败,原因[%s]", userName, loginResponsePacket.getReason()));
        }
    }
}
